<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 1000px;
            margin: 50px auto;
        }

        .table .item {
            float: left;
            width: 200px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: burlywood;
        }

        .table .item input {
            width: 100px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">密码</div>
                <div class="item">删除</div>
                <div class="item">编辑</div>
            </div>
            <div class="tr" v-for="item in list">
                <div class="item">
                    {{item.id}}</div>
                <div class="item">
                    <span v-if="!item.checked">{{item.username}}</span>
                    <span v-else><input v-model="item.username" /></span>

                </div>
                <div class="item">
                    <span v-if="!item.checked">{{item.userpwd}}</span>
                    <span v-else><input v-model="item.userpwd" /></span>
                </div>
                <div class="item">删除</div>
                <div class="item">
                    <span v-if="!item.checked" @click="edit(item)">编辑</span>
                    <span v-else>
                        <button @click="save(item)">保存</button>
                        <button @click="cancel(item)">取消</button>
                    </span>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            list: [],
        },
        methods: {
            edit(info) {//info是一个对象  对象是引用类型
                info.checked = true;
                //把以后要用的数据 提前存起来
                info.editusername = info.username;
                info.edituserpwd = info.userpwd;

            },
           async save(item) {
                //item.checked = false;
                //查询用户名是否可用 (查到了是自己可以用  查不到可以用)
                let  {data:list} = await  axios({
                    method:"get",
                    url:"http://localhost:2345/userinfos",
                    params:{//只有get才是parmas  其余的都是data
                        username:item.username
                    }
                })
                //用户是唯一的 能查到也只会有一条数据
                if(list.length){//判断集合
                    let info  = list[0];//查到的数据
                    if(info.id !=item.id){

                        alert("该用户名已经存在");
                        return false;
                    }
                }

                //其他都是可以用
                //可用就保存(更新)
                await axios({
                    method:"patch",
                    url:"http://localhost:2345/userinfos/"+item.id,
                    data:{
                        username:item.username,
                        userpwd:item.userpwd   
                    }
                })

                item.checked = false;

            },
            cancel(item) {
                item.checked = false;
                item.username  = item.editusername;//取出来 还原
                item.userpwd = item.edituserpwd;
            }
        },
        async created() {
            let { data: list } = await axios({
                method: "get",
                url: "http://localhost:2345/userinfos",
                params: {}
            })
            this.list = list.map(item => {
                item.checked = false;
                return item;
            });
        }
    })
</script>

</html>